/*
 * @# PostController.java 2011-12-22 下午03:22:00
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.posts.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ianswer.common.util.Feedback;
import com.ianswer.common.util.net.Decoder;
import com.ianswer.common.util.time.SecondsUtils;
import com.ianswer.common.util.web.ServletUtils;
import com.ianswer.px.common.SessionContextHolder;
import com.ianswer.px.common.annotation.SessionAware;
import com.ianswer.px.posts.entity.Post;
import com.ianswer.px.posts.entity.PostData;
import com.ianswer.px.posts.service.PostService;
import com.ianswer.px.user.entity.UserBaseInfo;
import com.ianswer.px.user.redis.FigureRedis;

/**
 * @author alvin hwang
 */
@SessionAware
@Controller
public class PostController {

	private static final int max = 20;

	@Resource(name = "postService")
	PostService postService;

	/**
	 * 创建新的博文.
	 */
	@RequestMapping(value = "/post/create", method = RequestMethod.POST)
	public @ResponseBody
	Feedback create(Post p, HttpServletRequest request) {
		try {
			//TODO 需要输入合法性、过滤关键字 
			String decode_cont = Decoder.decode(p.getContent());
			Long userId = SessionContextHolder.currentUser().getUserId();
			p.setUserId(userId);
			p.setContent(decode_cont);
			p.setCreatedAt(SecondsUtils.seconds());
			p.setIpAddr(ServletUtils.getIpAddrLong(request));
			postService.insert(p);
			return Feedback.success("t");
		} catch (Exception e) {
			return Feedback.fail("发布博文失败");
		}

	}

	/**
	 * 获取未读博文数
	 * @param p
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/post/unread", method = RequestMethod.GET)
	public @ResponseBody
	Feedback getPostNums(Post p, HttpServletRequest request) {
		try {
			Long postCount = FigureRedis.getUnReadNum(SessionContextHolder.getUserId());
			return Feedback.success("t").setObj(postCount);
		} catch (Exception e) {
			return Feedback.fail("发布博文失败");
		}

	}

	/**
	 * 点击未读消息.
	 */
	@RequestMapping(value = "/post/news", method = RequestMethod.GET)
	public @ResponseBody
	List<PostData> news() {
		Long userId = SessionContextHolder.getUserId();
		List<PostData> postDatas = postService.query(userId);
		//清零
		FigureRedis.setZeroUnReadNum(userId);
		return postDatas;
	}

	/**
	 * 首页展示博文.
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(Model model, HttpServletRequest request) {
		int skip = 0;
		String rp = request.getParameter("rp");
		if (StringUtils.isNotBlank(rp)) {
			skip = Integer.parseInt(rp);
		}
		UserBaseInfo user = UserBaseInfo.get(SessionContextHolder.getUserId());
		List<PostData> posts = postService.query(SessionContextHolder.getUserId(), skip, max);

		model.addAttribute("user", user);
		model.addAttribute("posts", posts);
		return "user/home";
	}
}
